//
//  DBJSettingAndHelpTableViewController.m
//  DBJ
//
//  Created by 花化华 on 2016/11/12.
//  Copyright © 2016年 itcast. All rights reserved.
//
//延迟方法 去掉svp 提示的效果
#define delay(time) dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((time) * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{[SVProgressHUD dismiss];})
#import "DBJSettingAndHelpTableViewController.h"
#import <Masonry.h>
#import "DBJSettingAndHelpTableViewModel.h"
#import "DBJSettingAndHelpTableViewCell.h"
#import "DBJSAndHFeedbackVC.h"
#import "DBJSAndHExplainVC.h"
#import "DBJSAndHPasswordVC.h"
#import <SVProgressHUD.h>
#import "DBJUserDefaultsTool.h"

static NSString *CELL_ID =@"CELL_ID";

@interface DBJSettingAndHelpTableViewController ()<UITableViewDataSource,UITabBarDelegate>

//要清除的的数据大小
@property(nonatomic,assign)float fileSize;

//数据源
@property(nonatomic,strong)NSArray  *ArrList;

@end

@implementation DBJSettingAndHelpTableViewController

- (void)dbjSetupSubviews{
    [super dbjSetupSubviews];
    
    //self.view.backgroundColor = [UIColor redColor];
    //初始化界面
     [self setupUI];
    
    //获取数据源
    self.ArrList = [[DBJSettingAndHelpTableViewModel alloc]getSettingModelArr];
    
}
#pragma mark-初始化视图
-(void)setupUI
{
    //注册
    [self.tableView registerClass:[ DBJSettingAndHelpTableViewCell class] forCellReuseIdentifier:CELL_ID];
   
}

#pragma mark-代理方法的实现
/** 设置分组style的头间隔 */
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}

/** 当选择某一个cell时触发的代理方法 */
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DBJSAndHFeedbackVC *bfVC = [[DBJSAndHFeedbackVC alloc]init];
    
    switch ((unsigned long)indexPath.row) {
        case 0:{
            
            //意见反馈的跳转
         
            bfVC.title = @"意见反馈";
            
        [self.navigationController pushViewController:bfVC animated:YES];
        }
            break;
        case 1:{
            //加符号说明的跳转
            //DBJSAndHExplainVC *EVC = [[DBJSAndHExplainVC alloc]init];
           bfVC.title = @"加符号说明";
           
        [self.navigationController pushViewController:bfVC animated:YES];
            
        }
            break;
            
        case 2:{
            
            //修改密码的跳转
            DBJSAndHPasswordVC *PasswordVC = [[DBJSAndHPasswordVC alloc]init];
            
            [self.navigationController pushViewController:PasswordVC animated:YES];
        }
            
            break;
        case 3:{
            
            
            [self filePath];
            
            [self alertView];
            
        }
            
            break;
        case 4:{
         
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"您确定要注销？" message:nil preferredStyle:UIAlertControllerStyleAlert];
            [alert addAction:[UIAlertAction actionWithTitle:@"注销" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
                [DBJTools dbjShowSVSuccessTitle:@"注销成功" andTime:STANTIME];
                
                [DBJUserDefaultsTool removeObjectForKey:CURRENTID];
                //发送通知登录、DBJLeftBarController接收
                [[NSNotificationCenter defaultCenter] postNotificationName:LOGINSUCCESS object:nil userInfo:nil];
                
            }]];
            [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
            [self presentViewController:alert animated:YES completion:nil];
            
        }
            break;
            
        default:
            break;
    }
}
#pragma mark-清除缓存
// 显示缓存大小
-( float )filePath
{
    
    NSString * cachPath = [ NSSearchPathForDirectoriesInDomains ( NSCachesDirectory , NSUserDomainMask , YES ) firstObject ];
    
    return [ self folderSizeAtPath :cachPath];
    
}
//1:首先我们计算一下 单个文件的大小

- ( long long ) fileSizeAtPath:( NSString *) filePath{
    
    NSFileManager * manager = [ NSFileManager defaultManager ];
    
    if ([manager fileExistsAtPath :filePath]){
        
        return [[manager attributesOfItemAtPath :filePath error : nil ] fileSize ];
    }
    
    return 0 ;
    
}
//2:遍历文件夹获得文件夹大小

- ( float ) folderSizeAtPath:( NSString *) folderPath{
    
    NSFileManager * manager = [ NSFileManager defaultManager ];
    
    if (![manager fileExistsAtPath :folderPath]) return 0 ;
    
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath :folderPath] objectEnumerator ];
    
    NSString * fileName;
    
    long long folderSize = 0 ;
    
    while ((fileName = [childFilesEnumerator nextObject ]) != nil ){
        
        NSString * fileAbsolutePath = [folderPath stringByAppendingPathComponent :fileName];
        
        folderSize += [ self fileSizeAtPath :fileAbsolutePath];
        
    }
    
    self.fileSize = folderSize/( 1024.0 * 1024.0 );
    return folderSize/( 1024.0 * 1024.0 );
    
}

// 清理缓存

- (void)clearFile
{
    NSString * cachPath = [ NSSearchPathForDirectoriesInDomains ( NSCachesDirectory , NSUserDomainMask , YES ) firstObject ];
    
    NSArray * files = [[ NSFileManager defaultManager ] subpathsAtPath :cachPath];
    
    NSLog ( @"cachpath = %@" , cachPath);
    
    for ( NSString * p in files) {
        
        NSError * error = nil ;
        
        NSString * path = [cachPath stringByAppendingPathComponent :p];
        
        if ([[ NSFileManager defaultManager ] fileExistsAtPath :path]) {
            
            [[ NSFileManager defaultManager ] removeItemAtPath :path error :&error];
            
        }
        
    }
    
    [ self performSelectorOnMainThread : @selector (clearCachSuccess) withObject : nil waitUntilDone : YES ];
    NSLog(@"%@",[NSThread currentThread]);
}
-(void)clearCachSuccess
{
   
}

- (void)alertView
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"您确定要清理缓存？" message:[NSString stringWithFormat:@"缓存大小为%0.2fMB",self.fileSize] preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"清除" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [DBJTools dbjShowSVSuccessTitle:@"清理成功" andTime:STANTIME];
        
        //调用清理缓存
        [self clearFile];
        
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [self presentViewController:alert animated:YES completion:nil];
}

#pragma mark-数据源方法的实现
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.ArrList.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //创建cell
   DBJSettingAndHelpTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELL_ID forIndexPath:indexPath];
    
   //取模型
    DBJSettingAndHelpTableViewModel *model = self.ArrList[indexPath.row];
    
    //赋值
    cell.arrList = model;
    
     return cell;
}

- (void)dbjSetupNavigationItems{
    [super dbjSetupNavigationItems];
}

@end
